<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keywords">
          <el-input v-model="queryParams.keywords" placeholder="请输入关键字" clearable style="width: 200px" />
          <el-button type="primary" style="margin-left: 0; border-radius: 0" @click="handleQuery"> 搜索 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card shadow="never">
      <el-table v-loading="loading" :data="tableList" highlight-current-row border>
        <el-table-column key="projectNum" label="项目编号" align="center" prop="projectNum" min-width="200" />
        <el-table-column key="projectName" label="项目名称" align="center" prop="projectName" min-width="200" />
        <el-table-column key="paymentName" label="缴纳主体名称" align="center" prop="paymentName" min-width="120" />
        <el-table-column
          key="paymentCode"
          label="缴纳主体统一社会信用代码"
          min-width="200"
          align="center"
          prop="paymentCode"
        />
        <el-table-column key="statusName" label="状态" min-width="100" align="center" prop="statusName">
          <template #default="scope">
            <el-tag v-if="scope.row.status == 0" type="warning"> 待审核 </el-tag>
            <el-tag v-if="scope.row.status == 1"> 已退还 </el-tag>
            <el-tag v-if="scope.row.status == 2" type="danger"> 待退还 </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          key="notificationLetter"
          label="动用通知书文号"
          min-width="120"
          align="center"
          prop="notificationLetter"
        />
        <el-table-column
          key="guaranteeAccountCode"
          label="账户编号"
          align="center"
          min-width="130"
          prop="guaranteeAccountCode"
        />
        <el-table-column key="applyAmount" label="可退还金额" min-width="195" align="center" prop="applyAmount" />
        <el-table-column key="refundAmount" label="退还金额" min-width="140" align="center" prop="refundAmount" />
        <el-table-column key="refundTime" label="退还日期" min-width="160" align="center" prop="refundTime" />
        <el-table-column label="操作" fixed="right" max-width="260" min-width="160">
          <template #default="scope">
            <el-button
              v-if="permissions.audit && scope.row.status == 0"
              v-hasData="scope.row.hasEdit"
              link
              size="small"
              type="primary"
              @click="handelAudit(scope.row)"
            >
              审核
            </el-button>
            <el-button v-if="permissions.detail" link size="small" type="primary" @click="handelDetail(scope.row)">
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageIndex"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
      <audit v-if="dialogAudit.visible" :dialog-audit="dialogAudit" @send-close="handleSendClose"></audit>
      <detail v-if="dialogDetail.visible" :dialog-detail="dialogDetail" @send-close="handleSendClose"></detail>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import audit from "./refundAudit.vue";
import detail from "./refundDetail.vue";
import { QueryProjectGuaranteeRefund } from "@/api/guarantee/index";

const permissions = ref({
  audit: false,
  detail: false,
});

const currentSystem = ref(localStorage.getItem("usertypeName"));
const isLumpContractor = ref(localStorage.getItem("isLumpContractor"));
if (currentSystem.value == "SysUser") {
  permissions.value.audit = true;
  permissions.value.detail = true;
}

if (currentSystem.value == "Manager") {
}
if (currentSystem.value == "Constructor" || currentSystem.value == "Commander" || currentSystem.value == "Corp") {
}

const total = ref(0);
const loading = ref(false);
const queryFormRef = ref(ElForm);
const tableList = ref<any[]>();
const queryParams = reactive({
  pageIndex: 1,
  pageSize: 20,
  keywords: "",
});
const dialogAudit = reactive<DialogOption>({
  visible: false,
});
const dialogDetail = reactive<DialogOption>({
  visible: false,
});

const handelAudit = (row: any) => {
  dialogAudit.visible = true;
  dialogAudit.id = row.id;
  dialogAudit.title = "审核退还信息";
  dialogAudit.footBtn = "提交";
};
const handelDetail = (row: any) => {
  dialogDetail.visible = true;
  dialogDetail.id = row.id;
  dialogDetail.title = "详情";
};

const handleSendClose = function (needRefresh: boolean) {
  dialogAudit.visible = false;
  dialogAudit.id = undefined;
  if (needRefresh) {
    handleQuery();
  }
};

/* 列表查询 */
const handleQuery = async () => {
  loading.value = true;
  QueryProjectGuaranteeRefund(queryParams)
    .then((res) => {
      tableList.value = res.data.list;
      queryParams.pageIndex = res.data.pageNum;
      queryParams.pageSize = res.data.pageSize;
      total.value = res.data.total;
    })
    .finally(() => {
      loading.value = false;
    });
};

onMounted(() => {
  handleQuery();
});
</script>

<style lang="less" scoped>
:deep(.detail .el-dialog__body) {
  height: var(--mobileAddFormHeight);
  overflow-y: auto;
}
</style>
